package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.*;
import com.itlong.cloud.POJO.PO.PropertyParkingSpacePO;
import com.itlong.cloud.POJO.VO.property.PropertyParkingGetVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyParkingDao;
import com.itlong.cloud.property.service.IPropertyParkingService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <desc>
 *      物业管理-车位管理服务层接口实现类
 * </desc>
 *
 * @createDate 2018/08/17
 */
@Service
public class PropertyParkingServiceImpl implements IPropertyParkingService {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyParkingServiceImpl.class);

    @Autowired
    private IPropertyParkingDao iPropertyParkingDao;

    private static ExecutorService importAddressFixedThreadPool = Executors.newFixedThreadPool(12); //导入车位的线程池

    /**
     * <desc>
     *      添加车位信息
     * </desc>
     *
     * @param propertyParkingSaveDTO 添加车位信息数据DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveParking(PropertyParkingSaveDTO propertyParkingSaveDTO) throws Exception {
        Date nowTime = new Date();
        PropertyParkingSpacePO propertyParkingSpacePO = new PropertyParkingSpacePO();
        BeanUtils.copyProperties(propertyParkingSaveDTO,propertyParkingSpacePO);
        propertyParkingSpacePO.setParkingId(LogicIdUtil.bussinessId());
        propertyParkingSpacePO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyParkingSpacePO.setCreateTime(nowTime);
        propertyParkingSpacePO.setUpdateTime(nowTime);
        return iPropertyParkingDao.saveParking(SqlUtil.durableData(propertyParkingSpacePO,PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      列表查询车位信息
     * </desc>
     *
     * @param propertyParkingGetListDTO 列表查询车位信息数据DTO
     * @return 分页结果
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyParkingGetVO> getParkingsByPage(PropertyParkingGetListDTO propertyParkingGetListDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyParkingGetListDTO);
        Page<PropertyParkingGetVO> page = new Page<>();
        page.getPage().setPageSize(propertyParkingGetListDTO.getPageSize());
        page.getPage().setCurrentPage(propertyParkingGetListDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<PropertyParkingGetVO> propertyParkingGetVOS = iPropertyParkingDao.getParkingsByPage(params);
        page.setRows(propertyParkingGetVOS,iPropertyParkingDao.getParkingsRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取单个车位信息
     * </desc>
     *
     * @param propertyParkingGetOneDTO 查询参数
     * @return 单个车位信息
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyParkingGetVO getOneParking(PropertyParkingGetOneDTO propertyParkingGetOneDTO) throws Exception {
        PropertyParkingGetVO propertyParkingGetVO = iPropertyParkingDao.getOneParking(SqlUtil.convertBeanToMapParams(propertyParkingGetOneDTO));
        return propertyParkingGetVO;
    }

    /**
     * <desc>
     *      修改车位信息
     * </desc>
     *
     * @param propertyParkingUpdateDTO 修改参数
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateParking(PropertyParkingUpdateDTO propertyParkingUpdateDTO) throws Exception {
        Date notTime = new Date();
        PropertyParkingSpacePO propertyParkingSpacePO = new PropertyParkingSpacePO();
        BeanUtils.copyProperties(propertyParkingUpdateDTO,propertyParkingSpacePO);
        // 项目id不改动
        propertyParkingSpacePO.setProjectId(null);
        propertyParkingSpacePO.setUpdateTime(notTime);
        Map<String,Object> map = SqlUtil.durableData(propertyParkingSpacePO,PlatformConstants.TABLE_UPDATE);
        return iPropertyParkingDao.updateParking(map);
    }

    /**
     * <desc>
     *      批量删除车位信息
     * </desc>
     *
     * @param parkingIds 车位id，多个用英文","相连
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deleteParkings(String parkingIds) throws Exception {
        List<String> parkingIdList = StringHandlerUtil.splitStringList(parkingIds);
        return iPropertyParkingDao.deleteParkings(parkingIdList);
    }

    /**
     * <desc>
     *      查询所有车位信息
     * </desc>
     *
     * @param propertyParkingGetListDTO 查询参数
     * @return 结果集
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyParkingGetVO> getAllParkings(PropertyParkingGetListDTO propertyParkingGetListDTO) throws Exception {
        if (StringUtils.isBlank(propertyParkingGetListDTO.getParkingIds())) {
            Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyParkingGetListDTO);
            return iPropertyParkingDao.getAllParkings(params);
        }
        List<String> parkingIds = StringHandlerUtil.splitStringList(propertyParkingGetListDTO.getParkingIds());
        return iPropertyParkingDao.getParkingsByIds(parkingIds);
    }

    /**
     * <desc>
     *      批量导入车位信息
     * </desc>
     *
     * @param projectId                  项目id
     * @param propertyParkingSaveDTOList 车位信息集
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/08/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer batchSaveParkings(String projectId, List<PropertyParkingSaveDTO> propertyParkingSaveDTOList) {
        List<PropertyParkingSpacePO> propertyParkingSpacePOS = new ArrayList<>();
        importAddressFixedThreadPool.execute(()-> {
            try {
                Date nowTime = new Date();
                for (PropertyParkingSaveDTO propertyParkingSaveDTO : propertyParkingSaveDTOList) {
                    PropertyParkingSpacePO propertyParkingSpacePO = new PropertyParkingSpacePO();
                    BeanUtils.copyProperties(propertyParkingSaveDTO,propertyParkingSpacePO);
                    propertyParkingSpacePO.setParkingId(LogicIdUtil.bussinessId());
                    propertyParkingSpacePO.setProjectId(projectId);
                    propertyParkingSpacePO.setStatus(DataStatusEnum.NORMAL.getType());
                    propertyParkingSpacePO.setCreateTime(nowTime);
                    propertyParkingSpacePO.setUpdateTime(nowTime);
                    propertyParkingSpacePOS.add(propertyParkingSpacePO);
                }
                iPropertyParkingDao.batchSaveParkings(propertyParkingSpacePOS);
            } catch (Exception e) {
                LOG.error("导入车位信息失败", e);
            }
        });
        return propertyParkingSaveDTOList.size();
    }
}
